package leetcode

import (
	"math"
	"sort"
)

//纯数学题
func visiblePoints(points [][]int, angle int, location []int) int {
	n := len(points)
	var angles []float64
	carry := 0
	//点和原点角度统计
	for i := 0; i < n; i++ {
		diff_x, diff_y := points[i][0]-location[0], points[i][1]-location[1]
		a := math.Atan2(float64(diff_y), float64(diff_x))/math.Pi*180
		if diff_y == 0 && diff_x == 0 {
			carry++
		} else {
			angles = append(angles, a)
		}
	}
	//排序
	sort.Float64s(angles)

	m := len(angles)
	for i := 0; i < m; i++ {
		angles = append(angles, angles[i]+360)
	}
	//双指针
	maxNums, left, right := 0, 0, 0
	for left < m {
		if angles[right]-angles[left] <= float64(angle) {
			right++
		} else {
			maxNums = max(right-left, maxNums)
			left++
		}
	}
	return maxNums + carry
}